<template>
    <div class="login">
        <div class="box">
            <p class="circle">
                <img src="@/assets/logo.png" alt="">
            </p>
            <el-form class="fromBox" :rules="rules" :model="ruleForm" ref="formRef">
                <el-form-item prop="username">
                    <el-input placeholder="请输入用户名" prefix-icon="el-icon-user-solid" class="username"
                        v-model="ruleForm.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" prefix-icon="el-icon-s-goods" class="password"
                        v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item class="foot">
                    <el-button type="primary" @click="login">登录</el-button>
                    <el-button type="info" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { mapState,mapMutations } from 'vuex';
export default {
    data() {
        return {
            fullPath:'',
            ruleForm: {
                username: '',
                password: '',
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        ...mapMutations(['addToken']),
        login() {
            this.$refs.formRef.validate(async (valid) => {
                if (!valid) {
                    return false
                } 
                let {data} = await this.$http.post('login',this.ruleForm)
                if(data.meta.status==200){
                    this.$message.success(data.meta.msg)
                    this.addToken(data.data)
                    if(this.fullPath!='/'){
                        this.$router.push(this.fullPath)
                    }else{
                        this.$router.push('/')
                    }
                }else{
                    this.$message.error(data.meta.msg)
                }
            });
        },
        reset(){
            this.$refs.formRef.resetFields();
           
        }
    },
    beforeRouteEnter(to,from,next){
        next((vm)=>{
            vm.fullPath = from.fullPath
        })
    }
}
</script>

<style lang="scss" scoped>
.login {
    width: 100%;
    height: 100%;
    background-color: #2b4b6b;
    min-width: 720px;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;

    .box {
        width: 450px;
        height: 300px;
        background: #fff;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        padding: 0 20px;
        padding-bottom: 10px;

        .fromBox {
            width: 100%;
            height: 60%;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
        }

        .username {
            width: 100%;
        }

        .password {
            width: 100%;
        }

        .foot {
            width: 100%;
            display: flex;
            justify-content: flex-end;
        }

        .circle {
            width: 130px;
            height: 130px;
            background: white;
            border-radius: 50%;
            position: absolute;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 999;
            position: relative;
            box-shadow: 0 0 10px #ccc;

            img {
                width: 80%;
                height: 80%;
                position: absolute;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background: #eee;
                box-sizing: border-box;
                border-radius: 50%;
            }
        }
    }
}
</style>